package com.project.communitymanagement.service.impl;

import com.project.communitymanagement.common.BaseServiceImpl;
import com.project.communitymanagement.common.Page;
import com.project.communitymanagement.controller.TbOwnerController;
import com.project.communitymanagement.entity.TbApprovaDts;
import com.project.communitymanagement.entity.TbLeaveInfo;
import com.project.communitymanagement.entity.TbOwner;
import com.project.communitymanagement.mapper.TbApprovaDtsMapper;
import com.project.communitymanagement.mapper.TbLeaveInfoMapper;
import com.project.communitymanagement.mapper.TbOwnerMapper;
import com.project.communitymanagement.service.TbLeaveInfoService;
import com.project.communitymanagement.service.TbOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbLeaveInfoServiceImpl  extends BaseServiceImpl<TbLeaveInfoMapper, TbLeaveInfo> implements TbLeaveInfoService {

    @Autowired
    private TbLeaveInfoMapper tbleaveinfoMapper;

    @Autowired
    private TbApprovaDtsMapper tbApprovaDtsMapper;
    @Autowired
    private TbOwnerService tbOwnerService;
    @Autowired
    private TbOwnerMapper tbOwnerMapper;

    @Override
    protected TbLeaveInfoMapper mapper() {
        return tbleaveinfoMapper;
    }

    @Override
    protected TbLeaveInfo defaultModel() {
        return new TbLeaveInfo();
    }


    @Override
    public Page<TbLeaveInfo> getPages(Page<TbLeaveInfo> page) {
        TbOwner userName = tbOwnerService.getUserName();
        page.getRequestData().setUser_id(Integer.parseInt(userName.getId()));
        return super.getPages(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(TbLeaveInfo tbLeaveInfo) {
        int save = super.save(tbLeaveInfo);
        /**增加审批信息*/
        TbApprovaDts tbApprovaDts = new TbApprovaDts();
        tbApprovaDts.saveInit();
        tbApprovaDts.setContent("请假申请");
        tbApprovaDts.setStatus(0);
        tbApprovaDts.setLeave_id(Integer.parseInt(tbLeaveInfo.getId()));
        tbApprovaDtsMapper.insert(tbApprovaDts);
        return save;
    }

    @Override
    public List<TbLeaveInfo> approvalList() {
        TbOwner userName = tbOwnerService.getUserName();
        return tbleaveinfoMapper.getDataByApprovalId(userName.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(TbLeaveInfo tbLeaveInfo) {
        if(tbLeaveInfo.getApproval_state() == 1){
            /**增加审批信息*/
            TbApprovaDts tbApprovaDts = new TbApprovaDts();
            tbApprovaDts.saveInit();
            tbApprovaDts.setContent(tbLeaveInfo.getContent_approval());
            tbApprovaDts.setStatus(tbLeaveInfo.getStatus());
            tbApprovaDts.setLeave_id(Integer.parseInt(tbLeaveInfo.getId()));
            tbApprovaDtsMapper.insert(tbApprovaDts);
        }
        return super.edit(tbLeaveInfo);
    }
}
